package com.example.demo01.controller;

import com.example.demo01.entity.Dept;
import com.example.demo01.entity.Result;
import com.example.demo01.service.IDeptService;
import com.example.demo01.service.impl.DeptServiceImpl;
import com.sun.net.httpserver.Authenticator;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 苏阳奕
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/depts")
@Tag(name = "部门管理")
public class DeptController {

    @Autowired
    private DeptServiceImpl deptService;

    @GetMapping
    public Result<List<Dept>> getDeptList() {
        return Result.success(deptService.list());
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        boolean save = deptService.save(dept);
        if (save) {
            return Result.success("添加成功");
        }
        return Result.fail("添加失败");
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        boolean b = deptService.updateById(dept);
        if (b) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean b = deptService.removeById(id);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }


}
